function inputNumber(value, decimal) {
  value = value
    .replace(/[^\d.-]/g, '')
    .replace(/([\d.-])-/g, '$1')
    .replace(/^(-?)0(\d)/g, '$1$2')
    .replace(/^(-?)[.]/g, '$1')
    .replace(/([\d][.]([\d]+)?)[.]/g, '$1')
  return decimal === +decimal ? value.replace(new RegExp(`([\\d][.]\\d{${decimal}})([\\d]+)`, 'g'), '$1') : value
}
function inputPositive(value, decimal) {
  value = value
    .replace(/[^\d.]/g, '')
    .replace(/^0(\d)/g, '$1')
    .replace(/^[.]/g, '')
    .replace(/([\d][.]([\d]+)?)[.]/g, '$1')
  return decimal === +decimal ? value.replace(new RegExp(`([\\d][.]\\d{${decimal}})([\\d]+)`, 'g'), '$1') : value
}
function inputNatural(value) {
  return value.replace(/[^\d]/g, '').replace(/^0(\d)/g, '$1')
}
function inputInteger(value) {
  return value
    .replace(/[^\d-]/g, '')
    .replace(/([\d-])-/g, '$1')
    .replace(/^(-?)0(\d)/g, '$1$2')
}

const limiterMap = {
  number: inputNumber,
  positive: inputPositive,
  natural: inputNatural,
  integer: inputInteger,
}

export default function limitInput(limiter, ...rest) {
  if (!limiterMap[limiter]) throw new Error('az-input-limiter: Invalid or unexpected limiter')
  return limiterMap[limiter](...rest)
}
